﻿PuppetPlay.ControlStick = function(config){
    Kinetic.Group.apply(this, [config]);
    this._initComponents();
}
PuppetPlay.ControlStick.prototype = {
    _initComponents: function () {
        var that = this;
		
		var rotationBoard = new PuppetPlay.RotationBoard({
            id: that.attrs.id + '_rotationboard',
            x: -63,
            y: 13,
            proxyPosition: {
                x: this.attrs.x
            },
            //y: PuppetPlay.Setting.MOVEHANDLE_CENTER_OFFSET_Y,
            afterRotate: function (deg) {
                if (that.attrs.afterRotate) {
                    that.attrs.afterRotate(deg);
                }
            },
            pointerDrawFunc: this.attrs.pointerDrawFunc
        });
        that.add(rotationBoard);

        var moveHandle = new PuppetPlay.MoveHandle({
            id: that.attrs.id + '_movehandle',
            proxyPosition: {
                x: this.attrs.x
            },
            y: PuppetPlay.Setting.MOVEHANDLE_CENTER_OFFSET_Y,
            beforeMove: this.attrs.beforeMove,
            afterMove: function (moveXY) {
                var rotateProxy = rotationBoard.proxy;
                that.move(moveXY);
                $(rotateProxy).css('left', that.getAbsolutePosition().x - $(rotateProxy).width()/2);
                if (that.attrs.afterMove) {
                    that.attrs.afterMove(moveXY);
                }
            }
        });
        that.moveHandle = moveHandle;
        that.add(moveHandle);
    },
    
    getJointPoint: function () {
        var center = this.moveHandle.getAbsolutePosition();
        var round = PuppetPlay.Util.round;
        return {
            x: round(center.x),
            y: round(center.y)
        };
    }
};
Kinetic.Global.extend(PuppetPlay.ControlStick, Kinetic.Group);